# -*- coding: utf-8 -*-

import sys
sys.path.append('../ReadData/')

from _read_xml_ import *
from feature import *
                             

# -----------------------------------------------------------------------------
# make feature vector from sentence pair and
# convert those to string of Classias format.
# -----------------------------------------------------------------------------
def convert(pair):
    feat = [ '%s:%lf' % (k, v) for k, v in getFeature(pair).iteritems() ]
    
    label = ' '
    if pair.attr.has_key('label'):
        label = pair.attr['label']
        if   label == 'Y': label = '+1';
        elif label == 'N': label = '-1';

    out = '%s %s' % ( ('F' if label=='B' else label), ' '.join(feat) )

    if label == 'B':
        feat = [ '%s:%lf' % (k, v)
                 for k, v in getFeature(pair.reversed()).iteritems() ]
        out += '\nF %s' % ' '.join(feat)

    return out


# -----------------------------------------------------------------------------
# convert feature vector of each of data_list to string of Classias format.
# output those to file.
# if reverse is true, the text and hypothesis are reversed.
# -----------------------------------------------------------------------------
def writeFeatures(filename, data_list):
    print 'start : convert features'
    print ' '*4 + 'filename =', filename
    
    with open(filename, 'w') as fo:
        for p in data_list:
            fo.write(convert(p) + '\n')

    print 'complete : convert features'


# -----------------------------------------------------------------------------
# get indices of data has B label in data_list.
# -----------------------------------------------------------------------------
def getIndicesLabelB(data_list):
    out = []
    for i, p in enumerate(data_list):
        if p.attr.has_key('label'):
            if p.attr['label'] == 'B':
                out.append(i)
    return out

linkInput = "data\RITE2-UnitTest\input\RITE2_JA_dev_unittest\RITE2_JA_dev_unittest.parsed.unidic.xml"
linkOutput="data\RITE2-UnitTest\\features\\feature_train.txt"

import os
directory_path = os.getcwd()

import sys
sys.path.append('../ReadData/')
from _read_xml_ import *

if __name__ == '__main__':
    # get root path to ../javn/
    root_path = directory_path[:directory_path.find("javn")+5]
        
    inputFile = root_path+linkInput
    